package com.shou.controller.admin;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shou.controller.MyController;
import com.shou.dao.support.Page;
import com.shou.entity.ServeInfo;
import com.shou.entity.ServeType;
import com.shou.service.impl.ServeInfoService;

@Controller("adminServeController")
@RequestMapping("/admin/serveInfo")
public class ServeController extends MyController{

	@Autowired
	ServeInfoService serveInfoService;
	
	@RequestMapping("/index")
	public String index(Integer pageSize,Integer pageNum,Model model) {
		
		Page<ServeInfo> page = serveInfoService.getServePage(pageNum, pageSize,null, null);
		model.addAttribute("page",page);
		return "/admin/serveInfo/index";
	}
	
	/**
	 * 服务信息详情
	 * @param model
	 * @param serveId
	 * @return
	 */
	@RequestMapping("/serveInfoDetail")
	public String serveInfoDetail(Model model,Long serveId){
		ServeInfo serveInfo = serveInfoService.getServeInfo(serveId);
		model.addAttribute("serveInfo", serveInfo);
		return "/admin/serveInfo/serveInfoDetail";
	}
		
	/**
	 * 服务类型列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/serveTypeList")
	public String serveTypeList(Model model) {
		model.addAttribute("typeList",serveInfoService.getServeTypeList());
		return "/admin/serveInfo/serveTypeList";
	}
	
	/**
	 * 编辑服务类型
	 * @param model
	 * @param serveTypeId
	 * @return
	 */
	@RequestMapping("/editServeType")
	public String addServeTypeList(Model model,Integer serveTypeId){
		ServeType type = null;
		if(serveTypeId != null) {
			type = serveInfoService.getServeType(serveTypeId);			
		}
		model.addAttribute("serveType", type);
		return "/admin/serveInfo/editServeType";
	}
	
	/**
	 * 保存服务类型
	 * @param serveType
	 * @return
	 */
	@RequestMapping("/saveServeType")
	public ResponseEntity<String> saveServeType(ServeType serveType) {
		
		String msg = "保存成功";
		Boolean status = true;
		if(StringUtils.isBlank(serveType.getTypeName())){
			msg = "请填写服务名称";
			status = false;
		}
		else {			
			try{
				serveInfoService.saveServeType(serveType);
			}
			catch(Exception e){
				status = false;
				msg = "保存失败";
				log.error("添加serveType失败",e);
				e.printStackTrace();
			}
		}
		return renderMsg(status, msg);
	}
	
}
